package com.yksj.healthtalk.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yksj.healthtalk.entity.DoctorPatient;
import com.yksj.healthtalk.entity.MessageEntity;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.app.HTalkApplication;

public class DoctorSettingHisotryAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	public  List<DoctorPatient> entities=new ArrayList<DoctorPatient>();
	onClickFriendAttentionListener attentionListener;
	onClickFriendHeadListener clickFriendHeadListener;
	private final ImageLoader mImageLoader;
	private final Drawable maleDrawable,femalDrawable,dayTicketDrawable,monthTicketDrawable;
	private View clickView;
	private Drawable []  ds ;
	private Drawable unKownDrawable;
	public DoctorSettingHisotryAdapter(Context context,List<DoctorPatient> entities) {
		inflater = LayoutInflater.from(context);
		this.entities = entities;
		mImageLoader = ImageLoader.getInstance();
	
		femalDrawable = context.getResources().getDrawable(R.drawable.women);
		maleDrawable = context.getResources().getDrawable(R.drawable.man);
		unKownDrawable = context.getResources().getDrawable(R.drawable.sex_unknown);
		dayTicketDrawable = context.getResources().getDrawable(R.drawable.day_ticket);
		monthTicketDrawable = context.getResources().getDrawable(R.drawable.month_ticket);
		ds = new Drawable[]{null,dayTicketDrawable,monthTicketDrawable};
	}

	@Override
	public int getCount() {
		return entities.size();
	}

	@Override
	public Object getItem(int position) {
		return entities.get(position);
	}

	@Override 
	public long getItemId(int position) {
		return position;
	}
	
	
	public void setAdapterData(ArrayList<DoctorPatient> entitie){
		entities.clear();
		entities.addAll(entitie);
		notifyDataSetChanged();
	}
	
	public View getClickView(){
		return clickView;
	}
	
	public void setClickView(View v){
		this.clickView = v;
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final viewHolder holder;
		final ConcurrentHashMap<String, List<MessageEntity>> messageMap = HTalkApplication.getAppData().messageCllection;
		final DoctorPatient customerInfoEntity = entities.get(position);
		if (convertView ==null||convertView.getTag()==null) {
			holder = new viewHolder();
			convertView = inflater.inflate(R.layout.doctor_history_adapter_item,null) ;
			holder.head = (ImageView) convertView.findViewById(R.id.head_image);
			holder.personName = (TextView) convertView.findViewById(R.id.name);
			holder.headSex = (ImageView) convertView.findViewById(R.id.head_sex);
			holder.v = (ImageView) convertView.findViewById(R.id.v);
			holder.note = (TextView) convertView.findViewById(R.id.note);
			holder.message = (TextView) convertView.findViewById(R.id.item_dele);
			holder.service_count = (TextView)convertView.findViewById(R.id.doctor_service_count);
			holder.mesgCount = (TextView)convertView.findViewById(R.id.item_dele);
			convertView.setTag(holder);
		}else {
			holder = (viewHolder) convertView.getTag();
		}
		mImageLoader.displayImage(customerInfoEntity.getCustomerSex(),customerInfoEntity.getClientIconBackground(), holder.head);
		//姓名
		holder.personName.setText(customerInfoEntity.getCustomerNickname());
		
		//是否是医师沙龙
		if ("777".equals(customerInfoEntity.getRoleId())||"888".equals(customerInfoEntity.getRoleId())) {
			holder.v.setVisibility(View.VISIBLE);
		}else {
			holder.v.setVisibility(View.GONE);
		}
		holder.mesgCount.setVisibility(View.VISIBLE);
		if (messageMap.containsKey(customerInfoEntity.getCustomerId()) && messageMap.get(customerInfoEntity.getCustomerId()).size() != 0) {
			holder.mesgCount.setText(messageMap.get(customerInfoEntity.getCustomerId()).size()+"");
		}else {
			holder.mesgCount.setVisibility(View.GONE);
		}
		
		//签名
		holder.note.setText(customerInfoEntity.getPersonalNarrate());
		holder.service_count.setText(customerInfoEntity.getReqCount());
		
		//点击头像进入个人资料
		holder.head.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (clickFriendHeadListener != null) {
					clickFriendHeadListener.onClickFriendHead(customerInfoEntity , position);
				}
			}
		});
		return convertView;
	}
	
	private static  class viewHolder {
		private TextView note;//描述
		private ImageView head; // 头像
		private TextView service_count;//服务时间
		private TextView personName; // 名称
		private ImageView headSex; // 性别
		private ImageView v;
		private TextView message;//消息的图标
		private TextView attentionSalon;//服务状态
		private TextView mesgCount;
	}
	
	public interface onClickFriendHeadListener{
		void onClickFriendHead(DoctorPatient entity , int positon);
	}
	
	public void setonClickFriendHeadListener(onClickFriendHeadListener headListener){
		this.clickFriendHeadListener = headListener;
	};
	
	public interface onClickFriendAttentionListener{
		void onClickFriendAttention(DoctorPatient entity , int positon);
	}
	
	public void setonClickFriendAttentionListener(onClickFriendAttentionListener attentionListener){
		this.attentionListener = attentionListener;
	}

	public void updateData(List<DoctorPatient> response) {
		if(response!=null){
		if(response.size()<20)
		entities.clear();
		this.entities.addAll(response);
		}
	};
}
